package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.Order;
import com.itheima.service.localOrderService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @author: Bestom@23
 * @Date: 2020/10/08/11:55
 * @Description: 预约列表
 */

@RestController
@RequestMapping("/localOrder")
public class localOrderController {

    @Reference
    private localOrderService localOrderService;

    // 电话预约提交表单
    @RequestMapping("/submit")
    public Result submit(@RequestBody Map map, Integer[] setmealIds) {

        map.put("orderType", Order.ORDERTYPE_TELEPHONE);
        System.out.println(map.get("orderDate"));
        try {
            Result result = localOrderService.submit(map, setmealIds);

            if (result.isFlag()) {
                // 电话预约成功
                return new Result(true, MessageConstant.TELEPHONE_APPOINTMENT_SUCCESS);

            }
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.TELEPHONE_APPOINTMENT_FAIL);
        }
    }


    /**
     * 动态开关
     *
     * @param map
     * @return
     */
    @RequestMapping("/switchStatusChange")
    public Result switchStatusChange(@RequestBody Map map) {


        try {
            localOrderService.switchStatusChange(map);
            return new Result(true, MessageConstant.MODIFIED_SUCCESS);
        } catch (Exception e) {
            return new Result(false, MessageConstant.MODIFIED_FAIL);
        }
    }


    @RequestMapping("/findById")
    public Result findById(Integer id) {
        try {
            Map result = localOrderService.findById(id);
            return new Result(true, MessageConstant.EDIT_APPOINTMENT_MANAGEMENT_SUCCESS, result);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_APPOINTMENT_MANAGEMENT_FAIL);
        }
    }


    // 删除电话预约项
    @RequestMapping("/deleteOrderById")
    public Result deleteOrderById(Integer id) {
        try {
            localOrderService.deleteOrderById(id);
            return new Result(true, MessageConstant.APPOINTMENT_MANAGEMENT_SUCCESS);
        } catch (Exception e) {
            return new Result(false, MessageConstant.APPOINTMENT_MANAGEMENT_FAIL);
        }
    }
    /**
     * 查询所有套餐数据
     * @return
     */
    @RequestMapping("/findIdsByOrderId")
    public Result findIdsByOrderId(Integer id) {
        try {
            List<Integer> ids = localOrderService.findIdsByOrderId(id);
            return new Result(true,MessageConstant.QUERY_PACKAGE_ITEM_SUCCESS,ids);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_PACKAGE_ITEM__FAIL);
        }
    }

    @RequestMapping("edit")
    public Result edit(@RequestBody Map map,Integer[] setmealIds){
        try {
            localOrderService.edit(map,setmealIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_APPOINTMENT_MANAGEMENT_SUCCESS);
        }
        return new Result(true,MessageConstant.EDIT_APPOINTMENT_MANAGEMENT_FAIL);
    }

}
